var util=require('util.js')

var addFoodCart=function(id,num,callback){
  var buynum = 0, buyamount = 0;
  var foodlist=wx.getStorageSync("foodlist");
  for (var i = 0; i < foodlist.length;i++){
    
    if (id==foodlist[i].id){
      if (foodlist[i].num == 0) {
        foodlist[i].addTime = new Date().getTime();
      }
      if (foodlist[i].num + num <0){
        util.showinfo("数量不能小于0"); 
        return;
      }
      foodlist[i].num = foodlist[i].num+num;
      foodlist[i].selected=true;
      
    }
    if (foodlist[i].num>0){
      buynum += Number(foodlist[i].num);
      buyamount += Number(foodlist[i].num) * Number(foodlist[i].price)
    }
  }
  var data = { buynum: buynum, buyamount: buyamount }
  util.setTabBadge(1, buynum);
  wx.setStorageSync("foodlist", foodlist);
  wx.setStorageSync("buycart",data)
  if (callback) {
    callback(foodlist,data)
  }else{
    return {"foodlist":foodlist,"buycart":data};
  }  
}
var selectedFoodCart=function(id,callback){
  var buynum = 0, buyamount = 0;
  var foodlist = wx.getStorageSync("foodlist");
  for (var i = 0; i < foodlist.length; i++) {
    if (id===foodlist[i].id){
      foodlist[i].selected = !foodlist[i].selected;
    } 
    if (foodlist[i].num > 0 && foodlist[i].selected){      
      buynum += Number(foodlist[i].num);
      buyamount += Number(foodlist[i].num) * Number(foodlist[i].price)
    }    
  }
  var data = { buynum: buynum, buyamount: buyamount}
  wx.setStorageSync("foodlist", foodlist);
  wx.setStorageSync("buycart", data)
  if (callback) {
    callback(foodlist, data)
  } else {
    return { "foodlist": foodlist, "buycart": data };
  }
}
var oldFoodToNew=function(foodlist,callback){
  var buynum = 0, buyamount = 0;
  var oldlist = wx.getStorageSync("foodlist");
  for(var i=0;i<oldlist.length;i++){
    if (oldlist[i].num>0){
      for (var j=0;j<foodlist.length;j++){
        foodlist[j].show=true;
        if (foodlist[j].id == oldlist[i].id){
          foodlist[j].addTime = oldlist[i].addTime;
          foodlist[j].num = oldlist[i].num;
          foodlist[j].selected=true;
          buynum += Number(foodlist[j].num);
          buyamount += Number(foodlist[j].num) * Number(foodlist[j].price)
        }
      }
    }
  }
  var data = { buynum: buynum, buyamount: buyamount }
  wx.setStorageSync("foodlist", foodlist);
  util.setTabBadge(1, buynum);
  if (callback) {
    callback(foodlist, data)
  } else {
    return { "foodlist": foodlist, "buycart": data };
  }
}
var getFoodlist=function(callback){
  var buynum = 0, buyamount = 0;
  var foodlist = wx.getStorageSync("foodlist");
  for (var i = 0; i < foodlist.length; i++) {
    if (foodlist[i].num > 0 && foodlist[i].selected) {
      buynum += Number(foodlist[i].num);
      buyamount += Number(foodlist[i].num) * Number(foodlist[i].price)
    }
  }
  var data = { buynum: buynum, buyamount: buyamount }
  wx.setStorageSync("foodlist", foodlist);
  wx.setStorageSync("buycart", data)
  util.setTabBadge(1, buynum);
  if (callback) {
    callback(foodlist, data)
  } else {
    return { "foodlist": foodlist, "buycart": data };
  }
}
var initFoodList=function(){
  var foodlist = wx.getStorageSync("foodlist");
  for (var i = 0; i < foodlist.length; i++) {
    foodlist[i].selected=false;
    foodlist[i].num=0;
  }
  wx.setStorageSync("foodlist", foodlist); 
}
var getCartList=function(){
  var foodlist = wx.getStorageSync("foodlist");
  var cartlist=[];
  for (var i = 0; i < foodlist.length; i++) {
    if (foodlist[i].num>0){
      cartlist.push(foodlist[i]);
    }
  }
  cartlist.sort(function(a,b){
    return b.addTime-a.addTime;
  })
  return cartlist;
}
module.exports={
  addFoodCart: addFoodCart,
  selectedFoodCart: selectedFoodCart,
  oldFoodToNew: oldFoodToNew,
  getFoodlist: getFoodlist,
  initFoodList: initFoodList,
  getCartList: getCartList
}
